//
//  NetWorkRootViewController.m
//  CPICApp
//
//  Created by sinosoft on 13-3-22.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "NetWorkRootViewController.h"
#import "MYPoint.h"

@interface NetWorkRootViewController ()

@end

@implementation NetWorkRootViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
    ADD_LEFT_BUTTONITEM(@"back.png");
    ADD_TITLE_NAMEITEM(@"服务网点查询");
    
    myMapView = [[[MAMapView alloc] initWithFrame:CGRectMake(0, 44, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 44)] autorelease];
	myMapView.mapType = MAMapTypeStandard;
    myMapView.showsUserLocation=YES;
    myMapView.delegate = self;
    
    CLLocationCoordinate2D center = {39.91669,116.39716};
    MACoordinateSpan span = {0.04,0.03};
    MACoordinateRegion region = {center,span};
    [myMapView setRegion:region animated:NO];
    [self.view addSubview:myMapView];
	[self addPoint];
	[self addLine];
	[self addPolygon];
	[self addCircle];
}
-(void) ClickCallOutBtn
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)toggleMapType:(id)sender
{
    myMapView.mapType=(MAMapType) [(UISegmentedControl *)sender selectedSegmentIndex];
}

- (void)addPoint//添加标注
{
	MAPointAnnotation *pointAnn = [[[MAPointAnnotation alloc]init]autorelease];
	pointAnn.coordinate = CLLocationCoordinate2DMake(39.91669, 116.39716);
	[pointAnn setTitle:@"MAPointAnnotation"];
	[myMapView addAnnotation:pointAnn];
	pa = [[MAPointAnnotation alloc]init];
	pa.coordinate = CLLocationCoordinate2DMake(39.92669, 116.38716);
	[pa setTitle:@"point to remove"];
	[myMapView addAnnotation:pa];
}
-(void)addPoint:(id)sender
{
    [myMapView removeAnnotation:pa];//移除标注
}

- (void)addLine//绘制直线
{
	CLLocationCoordinate2D coords[2];
	coords[0] = CLLocationCoordinate2DMake(39.91669, 116.39716);
	coords[1] = CLLocationCoordinate2DMake(39.92669, 116.38716);
	MAPolyline *line = [MAPolyline polylineWithCoordinates:coords count:2];
    
	[myMapView addOverlay:line];
}

- (void)addPolygon//绘制多边形
{
	CLLocationCoordinate2D coords[3];
	coords[0] = CLLocationCoordinate2DMake(39.93000, 116.38500);
	coords[1] = CLLocationCoordinate2DMake(39.91000, 116.39700);
	coords[2] = CLLocationCoordinate2DMake(39.91000, 116.38000);
    
	MAPolygon *polygon = [MAPolygon polygonWithCoordinates:coords count:3];
	NSLog(@"%f", myMapView.frame.origin.x );
	[myMapView addOverlay:polygon];
}

- (void)addCircle//绘制圆
{
	CLLocationCoordinate2D center = CLLocationCoordinate2DMake(39.91000, 116.40000);
	CLLocationDistance radius = 500;
	MACircle *circle= [MACircle circleWithCenterCoordinate:center radius:radius];
	[myMapView addOverlay:circle];
}

#pragma  #####------------
#pragma  MAMapViewDelegate
- (MAOverlayView*)mapView:(MAMapView *)mapView viewForOverlay:(id<MAOverlay>)overlay
{
	if ([overlay isKindOfClass:[MAPolyline class]]) {
		MAPolylineView *lineView = [[[MAPolylineView alloc] initWithPolyline:(MAPolyline *) overlay]autorelease];
        lineView.lineWidth = 5.0f;
        lineView.strokeColor = [UIColor redColor];
        lineView.fillColor = [UIColor blackColor];
        //lineView.lineDashPattern
		return lineView;
	}
	else if ([overlay isKindOfClass:[MAPolygon class]]) {
		MAPolygonView *polygonView = [[[MAPolygonView alloc]initWithPolygon:(MAPolygon *) overlay]autorelease];
		return polygonView;
	}
	else if ([overlay isKindOfClass:[MACircle class]]) {
		MACircleView *circleView = [[[MACircleView alloc]initWithCircle:(MACircle *) overlay]autorelease];
		return circleView;
	}
	return nil;
}

-(NSString*)keyForMap
{
    return @"c14816a5330213770ea772273a41bc24";//高德地图key
}

- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
	MAPinAnnotationView *pinAnnView = [[[MAPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pinAnnView"]autorelease];
	NSLog(@"annotation:%f,%f",annotation.coordinate.latitude,annotation.coordinate.longitude);
	pinAnnView.pinColor = MAPinAnnotationColorGreen;
	pinAnnView.animatesDrop = YES;
	pinAnnView.canShowCallout = YES;
	pinAnnView.draggable = YES;
	return pinAnnView;
}

- (void)mapView:(MAMapView *)mapView didAddAnnotationViews:(NSArray *)views{
	NSLog(@"added AnnotationViews");
}
- (void)mapViewWillStartLocatingUser:(MAMapView *)mapView NS_AVAILABLE(NA, 4_0){
    
    NSLog(@"++++startLocation");
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    [pa release];
    [myMapView release];
    [super dealloc];
}

@end
